var app = getApp()
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');
var formatTime = require('../widget/time');
var filter = require('../widget/filter');
var comment = require('../widget/comment/comment');
var goTop = require('../widget/goTop/goTop');

var shareContent = {
    'title':'企鹅优品－优质生活指南',
    'desc':'腾讯精品体验平台，为用户提供优质的生活方式和购物体验。',
    'path':'/pages/project-index/index'
}
var shareObj = utils.share(shareContent);
var pageSetting = {
    callback:function(componentName){
        var that = this;
        switch(componentName){
            case 'pullUpLoad':
                pullUpLoad();
            break;
        }

        //上拉加载
        function pullUpLoad(){
            that.setData({
                upLoadFlag:0
            });
            modal.loading();
            var listData = {
                'self:message_api:message:list':{
                    'page':that['data']['upLoadPage'],
                    'size':10
                }
            };

            request.post({
                path: '/wxapp_api/api'
            },listData)
            .then(function (res){
                if(res['self:message_api:message:list']['ret'] != 0){
                    // modal.toast(res['self:message_api:message:list']['msg']);
                    that.setData({
                        upLoadFlag:1
                    });
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }

                var newsData = res['self:message_api:message:list']['data'];
                if(newsData.length < 10){
                    that.setData({
                        upLoadFlag:0
                    });
                }else{
                    that.setData({
                        upLoadFlag:1
                    });
                }
                for(var i = 0; i< newsData.length;i++){
                    newsData[i]['message_time'] = formatTime.formatTime(newsData[i]['app_message_time']);
                    newsData[i]['mess_info'] = filter.filter(newsData[i]['mess_info']);
                    newsData[i]['relate_info'] = filter.filter(newsData[i]['relate_info']);
                    newsData[i]['id'] = filter.filterUrl('id',newsData[i]['relate_url']);
                }
                
                that['data']['newsData'] = that['data']['newsData'].concat(newsData);
                that.setData({
                    newsData:that['data']['newsData']
                });
                modal.hide();
        });
        }
    },
    data: {
        medalMaskFlag:false
    },
    onLoad: function() {
        modal.loading();
        var that = this;
        var listData = {
            'self:message_api:message:list':{
                'page':1,
                'size':10
            }
        }
        request.post({
            path: '/wxapp_api/api'
        },listData)
        .then(function (res){
            if(res['self:message_api:message:list']['ret'] != 0){
                // modal.toast(res['self:message_api:message:list']['msg']);
                wx.navigateTo({
                    url: "../error/error"
                })
                return false;
            }

            var newsData = res['self:message_api:message:list']['data'];
            if(newsData.length < 10){
                that.setData({
                    upLoadFlag:0
                });
            }
            for(var i = 0; i< newsData.length;i++){
                newsData[i]['message_time'] = formatTime.formatTime(newsData[i]['app_message_time']);
                newsData[i]['mess_info'] = filter.filter(newsData[i]['mess_info']);
                newsData[i]['relate_info'] = filter.filter(newsData[i]['relate_info']);
                newsData[i]['id'] = filter.filterUrl('id',newsData[i]['relate_url']);
            }
          
            that.setData({
                newsData:newsData
            });
            modal.hide();
        });
    },
    onShow:function(){
        
        var that = this;
        var listData = {
            'self:message_api:message:list': {
                'page': 1,
                'size': 10
            }
        }
        request.post({
            path: '/wxapp_api/api'
        }, listData)
            .then(function (res) {

                if (res['self:message_api:message:list']['ret'] != 0) {
                    // modal.toast(res['self:message_api:message:list']['msg']);
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }

                var newsData = res['self:message_api:message:list']['data'];
                if (newsData.length < 10) {
                    that.setData({
                        upLoadFlag: 0
                    });
                }
                for (var i = 0; i < newsData.length; i++) {
                    newsData[i]['message_time'] = formatTime.formatTime(newsData[i]['app_message_time']);
                    newsData[i]['mess_info'] = filter.filter(newsData[i]['mess_info']);
                    newsData[i]['relate_info'] = filter.filter(newsData[i]['relate_info']);
                    newsData[i]['id'] = filter.filterUrl('id', newsData[i]['relate_url']);
                }

                that.setData({
                    newsData: newsData
                });
                
            });
    },
    showMedalMask: function(){
        this.setData({
            medalMaskFlag : true
        });
    },
    hideMedalMask: function(){
        this.setData({
            medalMaskFlag : false
        });
    }
};

utils.assign(pageSetting,comment,goTop);
Page(pageSetting);